/*
 * Hibiscus is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * by the Free Software Foundation; either version 3 of the License,
 * or (at your option) any later version.
 *
 * Hibiscus is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Hibiscus; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 *
 * Copyright (C) 2003 Tetete - http://tetete.hu.
 */
package hu.tetete.hibiscus.model;

import java.io.Serializable;
import java.util.List;

import hu.tetete.hibiscus.expression.Expression;
import hu.tetete.hibiscus.propertyvalue.SelectPropertyValue;

/**
 * TODO javadoc.
 */
public class OneSelectProperty extends Property<SelectPropertyValue> {

    /** @see Serializable */
    private static final long serialVersionUID = 1L;

    private final List<SelectPropertyValue> options;

    /**
     * @param id {@link Property#id}
     * @param name {@link Property#name}
     * @param expression {@link Property#expression}
     * @param options The selectable options relating to this property.
     */
    public OneSelectProperty(final Long id, final String name, final Expression expression, final List<SelectPropertyValue> options) {
        super(id, name, expression);
        this.options = options;
    }

    /**
     * @return the options
     */
    public List<SelectPropertyValue> getOptions() {
        return this.options;
    }

    /**
     * @see Property#fromDb(java.lang.String)
     */
    @Override
    public SelectPropertyValue fromDb(final String propertyValueFromDb) {
        final Long propertyValueId = Long.valueOf(propertyValueFromDb);
        for (final SelectPropertyValue option : this.options) {
            if (propertyValueId.equals(option.getId())) {
                return option;
            }
        }
        return null;
    }

    /**
     * @see Property#toDb(hu.tetete.hibiscus.propertyvalue.PropertyValue)
     */
    @Override
    public String toDb(final SelectPropertyValue propertyValue) {
        return propertyValue.getId().toString();
    }
}
